package ar.unc.famaf.pln.giveselector.context;

import give.formula.Atom;
import give.formula.Constant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * A response is a list of DF actions which are a reaction to a DG utterance.
 * It's the second part of an adjancency pair.
 * 
 * @author Luciana Benotti
 */

@SuppressWarnings("serial")
public class Response implements Serializable
{
	private List<Atom> executedActions; // list of actions executed by the DF as a response to the DG
										// utterance
	private List<Atom> discretizedActions; // plan that links the state when the DG said the
	// utterance with the state right before the following utterance
	
	public Response()
	{
		executedActions = new ArrayList<Atom>();
	}
	
	public void setDiscretizedActions(List<Atom> discretizedActions)
	{
		this.discretizedActions = discretizedActions;
	}
	
	public List<Atom> getDiscretizedActions()
	{
		return this.discretizedActions;
	}
	
	public void addAction(Atom header)
	{
		//Atom header = actionMsg.getHeader();
		executedActions.add(new Atom(header.getPredicate(), header.getArguments().get(0)));
	}
	
	/**
	 * Updates the Response by adding a new move action. If the given region is
	 * different from the current region, adds a move action.
	 */
	public void updateMove(String oldRegion, String newRegion)
	{
		if (!oldRegion.equals(newRegion))
			executedActions.add(new Atom("move", new Constant(oldRegion), new Constant(newRegion)));
	}

	
	public List<Atom> getExecutedActions()
	{
		return executedActions;
	}
	
	public String toString()
	{
		return "Executed Actions: " + executedActions + "\n" + "Discretized Reaction: " + this.discretizedActions;
		
		
	}

	public int length()
	{
		return this.executedActions.size();
	}
	
	public boolean isEmpty()
	{
		if (executedActions != null && !executedActions.isEmpty())
			return false;
		if (discretizedActions != null && !discretizedActions.isEmpty())
			return false;
		 
		return true;
	}

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + ((executedActions == null) ? 0 : executedActions.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Response other = (Response) obj;
		if (executedActions == null)
		{
			if (other.executedActions != null)
				return false;
		}
		else if (!executedActions.equals(other.executedActions))
			return false;
		return true;
	}
}
